import axios from 'axios';

const service = axios.create({
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  baseURL: process.env.VUE_APP_BASEH5_API,
  timeout: 60000,
  transformRequest: [(data) => {
    let tResult;
    if (data) {
      const keys = Object.keys(data);
      const t = [];
      keys.forEach((item) => {
        t.push(`${item}=${data[item]}`);
      });
      tResult = t.join('&');
    } else {
      tResult = data;
    }
    return tResult;
  }],
});

/* 过滤请求 */
service.interceptors.request.use((config) => {
  return config;
});

/* 过滤响应 */
service.interceptors.response.use((result) => {
  return result.data;
}, (err) => {
  return Promise.reject(err);
});

export default service;
